#include "main_header.h"

int Tcp_connect(char *fd,char *port){
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == 0){
        printf("get sockfd failed in init_Tcp_connect\n");
        exit(-1);
    }
    int flag = 1;
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(flag));

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    addr.sin_addr.s_addr = inet_addr(fd);
    int check_bind = bind(sockfd,(struct sockaddr *)&addr,sizeof(addr));
    if(check_bind == -1){
        printf("bind failed in init_Tcp_connect\n");
        exit(-1);
    }

    int check_listen = listen(sockfd,50);
    if(check_listen == -1){
        printf("listen failed in init_Tcp_connect\n");
        exit(-1);
    }
    return sockfd;
}


